package com.nine.gxh.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.ruoyi.common.constant.MyRabbitConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @RabbitListener 可以标注在类上面，需配合 @RabbitHandler 注解一起使用
 * @RabbitListener 标注在类上面表示当有收到消息的时候，就交给 @RabbitHandler 的方法处理，具体使用哪个方法处理，根据 MessageConverter 转换后的参数类型
 * <p>
 * 此处注意：如果标注在类上，一定要有对应的接收消息方法，类型要与发送的类型一致
 * @author: duonine
 * @date: 2023/7/5
 */
@Slf4j
@Component
public class GxhChannelConsumer {

    @RabbitListener(queues = MyRabbitConstants.CHANNEL_QUEUE_NAME)
    public void process(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        ObjectMapper mapper = new ObjectMapper();
        // 将 string 转为 map 对象
        Map<String, Object> map = mapper.readValue(msg, Map.class);
        for (String key : map.keySet()) {
            log.info("rabbitmq消费者消费消息：：" + key + "------" + map.get(key));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }

}
